<template>
  <el-breadcrumb class="app-breadcrumb" separator="/">
    <transition-group name="breadcrumb">
      <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
        <span v-if="item.redirect==='noRedirect'||index==levelList.length-1" class="no-redirect">
          {{ item.meta.title }}
        </span>
        <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
      </el-breadcrumb-item>
    </transition-group>
  </el-breadcrumb>
</template>

<script>

export default {
  data() {
    return {
      levelList: null
    }
  },
  watch: {
    $route(route) {
      // if you go to the redirect page, do not update the breadcrumbs
      if (route.path.startsWith('/redirect/')) {
        return
      }
      this.getBreadcrumb()
    }
  },
  created() {
    this.getBreadcrumb()
  },
  methods: {
    getBreadcrumb() {
      // only show routes with meta.title
      let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
      this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
    },
    handleLink(item) {
      const { params, query } = this.$route
      const { redirect, path } = item
      if (path == '/grouplist') {
        query.partnercorpid = undefined
        query.groupid = undefined
        query.mediaid = undefined
        query.mediatype = undefined
        query.seriesid = undefined
      } else if (path == '/medialist') {
        query.mediaid = undefined
        query.mediatype = undefined
        query.seriesid = undefined
        query.isedit = undefined
      } else if (path.indexOf('/constant/category') !== -1) {
        query.catid = undefined
        query.mediatype = undefined
      } else if(path == '/package') {
        query.packageid = undefined
        query.mediaid = undefined
      } else if (path.indexOf('/package/contentproject/media/') !== -1) {
        query.seriesid = undefined
      } else if (path.indexOf('/package/contentproject/index') !== -1) {
        query.mediaid = undefined
        query.mediatype = undefined
      } else if(path.indexOf('/package/content') !== -1) {
        query.mediaid = undefined
        query.mediatype = undefined
      } else if (path.indexOf('/review') !== -1) {
        query.mediaid = undefined
      } else if (path.indexOf('/fromcorpmedia/media') !== -1) {
        query.isedit = undefined
        query.seriesid = undefined
      } else if (path.indexOf('/fromcorpmedia') !== -1) {
        query.mediaid = undefined
      } else if(path.indexOf('/mediashare/media') !== -1) {
        query.isedit = undefined
        query.seriesid = undefined
      } else if (path.indexOf('/mediashare') !== -1) {
        query.isedit = undefined
      } else if (path.indexOf('/media/detail') !== -1) {
        query.seriesid = undefined
        query.isedit = undefined
      } else if (path.indexOf('/media/series/detail') !== -1) {
        query.isedit = undefined
      } else if (path.indexOf('/medias/list/all/detail') !== -1) {
        query.seriesid = undefined
      }  else if (path.indexOf('/medias/list/liveroom/detail') != -1) {
        query.seriesid = undefined
      } else if (path.indexOf('/medias/list/all')) {
        query.seriesid = undefined
        query.groupid = undefined
        query.mediatype = undefined
        query.seriesid = undefined
        query.mediaid = undefined
      } else if (path.indexOf('/medias/list/liveroom') !== -1) {
        query.seriesid = undefined
        query.groupid = undefined
        query.mediatype = undefined
        query.seriesid = undefined
        query.mediaid = undefined
      } else if (path.indexOf('/medias/list') !== -1) {
        query.groupid = undefined
        query.mediatype = undefined
        query.mediaid = undefined
      }
      if (redirect) {
        this.$router.push({path: redirect, params: params, query: query})
        return
      }

      this.$router.push({path: path, params: params, query: query})
      // this.$router.push(this.pathCompile(path))
    }
  }
}
</script>

<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
  display: inline-block;
  font-size: 14px;
  line-height: 30px;
  margin-left: 8px;

  .no-redirect {
    color: #97a8be;
    cursor: text;
  }
}
</style>
